package com.companion.lh.app

import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.amap.api.maps.MapView
import com.amap.api.maps.model.MyLocationStyle
import com.companion.lh.companion_app.R
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    private var mapView : MapView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mapView = activityMap as MapView
        mapView?.onCreate(savedInstanceState)
        //初始化地图
        initMap()

        addActivityBtn.setOnClickListener { toAddActivityPage() }
    }

    private fun toAddActivityPage(){
        val intent = Intent(this, ActivityActivity::class.java)
        startActivity(intent)
    }

    private fun initMap(){
        var aMap = mapView?.map
        var myLocationStyle = MyLocationStyle()
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW)
        myLocationStyle.interval(2000)
        aMap?.myLocationStyle = myLocationStyle
        aMap?.isMyLocationEnabled = true
    }

    override fun onDestroy() {
        super.onDestroy()
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        mapView?.onDestroy()
    }

    override fun onResume() {
        super.onResume()
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        mapView?.onResume()
    }

    override fun onPause() {
        super.onPause()
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        mapView?.onPause()
    }

    override fun onSaveInstanceState(outState: Bundle?) {
        super.onSaveInstanceState(outState)
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        mapView?.onSaveInstanceState(outState)
    }
}
